<template>
  <div>
    <div class="box-header">
      <el-breadcrumb separator="/">
        <el-breadcrumb-item>首页</el-breadcrumb-item>
        <el-breadcrumb-item>电子处方</el-breadcrumb-item>
        <el-breadcrumb-item>处方资料</el-breadcrumb-item>
      </el-breadcrumb>
    </div>
    <div>
      <el-form :inline="true" class="demo-form-inline">
        <el-form-item>
          <el-autocomplete
            v-model="formInline"
            :fetch-suggestions="querySearchAsync"
            placeholder="患者名称"
            :trigger-on-focus="false"
            @select="handleSelect">
          </el-autocomplete>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click.native="inquire(formInline)" icon="search">查询</el-button>
        </el-form-item>
      </el-form>  
    </div>
    <div>
      <western-medicine :visible="WesternMedicineVisible" :partner-id="recordId" @fromson="fromSon" judge="detailed">
      </western-medicine>
      <chinese-medicine :visible="ChineseMedicineVisible" :partner-id="recordId" @fromson="fromSon" judge="detailed">
      </chinese-medicine>
      <check-project :visible="CheckProjectVisible" :partner-id="recordId" @fromson="fromSon" judge="detailed">
      </check-project>
    </div>
    <div><!-- 处方信息表 -->
      <el-table :data="tableData" border stripe>
        <el-table-column inline-template label="姓名">
          <div>{{ row.header.partner_id[1] }}</div>
        </el-table-column>
        <el-table-column inline-template label="性别">
          <div>
            <div v-if="row.header.sex == 'm'">男</div>
            <div v-if="row.header.sex == 'w'">女</div>
          </div>
        </el-table-column>
        <el-table-column property="header.phone" label="联系电话"></el-table-column>
        <el-table-column property="header.name" label="处方名"></el-table-column>
        <el-table-column property="header.d_time" label="时间"></el-table-column>
        <el-table-column inline-template label="处方类型">
          <div>
            <div v-if="row.header.prescription_type == '0'">西药处方</div>
            <div v-if="row.header.prescription_type == '1'">中药处方</div>
            <div v-if="row.header.prescription_type == '2'">检查项目</div>
          </div>
        </el-table-column>
        <el-table-column property="header.department" label="科别"></el-table-column>
        <el-table-column inline-template label="诊断类型">
          <div>
            <div v-if="row.header.diagnostic_type == '0'">初诊</div>
            <div v-if="row.header.diagnostic_type == '1'">复诊</div>
          </div>
        </el-table-column>
        <el-table-column property="header.doctor" label="医生"></el-table-column>
        <el-table-column inline-template label="操作">
          <el-button type="primary" size="small"
            @click.native="detailed(row.header.id, row.header.prescription_type)">
            详细
          </el-button>
        </el-table-column>
      </el-table>
    </div>
    <div class="block">
      <el-pagination
        @current-change="handleCurrentChange"
        :current-page="1"
        :page-size="10"
        layout="total, prev, pager, next, jumper"
        :total="total">
      </el-pagination>
    </div>
  </div>
</template>

<script>
import WesternMedicine from './WesternMedicine.vue'
import ChineseMedicine from './ChineseMedicine.vue'
import CheckProject from './CheckProject.vue'
export default {
  data() {
    return {
      formInline: '',
      total: 1,
      tableData: [],
      queryStatus: '',
      WesternMedicineVisible: false,
      ChineseMedicineVisible: false,
      CheckProjectVisible: false,
      recordId: '',
      CurrPage: ''
    }
  },
  components: {
    WesternMedicine,
    ChineseMedicine,
    CheckProject
  },
  methods: {
    handleCurrentChange(val) {
      this.CurrPage = val
      if (this.queryStatus == '') {/*分页*/
        this.$http.get('/api/prescriptions/page/'+val).then(function (response) {
          this.total = response.data.recordcount
          this.tableData = response.data.data
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      } else {/*查询后分页*/
        let parameters = this.queryStatus+'/'+val
        this.$http.get('/api/prescriptions/'+parameters).then(function (response) {
          this.total = response.data.recordcount
          this.tableData = response.data.data
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      }
    },
    inquire(formInline) {/*查询*/
      if (formInline == '') {
        this.$message({
          showClose: true,
          message: '请输入查询条件',
          type: 'warning'
        })
      }
      else {
        this.$http.get('/api/prescriptions/'+formInline+'/1').then(function (response) {
          this.total = response.data.recordcount
          this.tableData = response.data.data
          this.queryStatus = formInline
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
        })
      }
    },
    querySearchAsync(queryString, funcd) {/*下拉列表数据查询*/
      if (queryString == '') {
        funcd()
        this.queryStatus = ''
        this.handleCurrentChange(1)
      }
      else {
        this.$http.get('/api/prescriptions/'+queryString+'/1').then(function (response) {
          let data = []
          let datas = response.data.data
          for (let i = 0; i < datas.length; i++) {
            data[i] = { 'value': datas[i].header.partner_id[1] }
          }
          if (data.length > 0) {
            funcd(data)  
          } else {
            data[0] = { 'value': '' }
            funcd(data)
          }          
        }, (response) => {
          this.$message({
            showClose: true,
            message: '服务器连接失败',
            type: 'error'
          })
          funcd()
        })       
      }
    },
    handleSelect(item) {
      this.inquire(item.value)
    },
    detailed(id, type) {
      if (type == '0') {
        this.recordId = id
        this.WesternMedicineVisible = true
      } else if (type == '1') {
        this.recordId = id
        this.ChineseMedicineVisible = true
      } else if (type == '2') {
        this.recordId = id
        this.CheckProjectVisible = true
      } else {
        this.$message({
          showClose: true,
          message: '该处方类型出错，请新建处方！',
          type: 'error'
        })
      }
    },
    fromSon() {
      this.WesternMedicineVisible = false
      this.ChineseMedicineVisible = false
      this.CheckProjectVisible = false
      this.handleCurrentChange(this.CurrPage)
    }
  },  
  mounted() {
    this.handleCurrentChange(1)
  }
}
</script>